// search.js
Page({
  data: {
    searchInput: '', // 输入的搜索内容
    searchHistory: [], // 搜索历史记录-动态变化
    searchHistorytemp: [],//暂存
    result: null,
    showTap: true,
    showresult: [false, false, false],//决定
    clear: true,
    index: 0,
    placeholder: "请输入您要搜索的内容",
    baseUrl:getApp().globalData.baseUrl
  },
  clickHistory(event){
    console.log(event.currentTarget.dataset)
    console.log(event.currentTarget.dataset.index)
    this.setData({
      searchInput:this.data.searchHistory[event.currentTarget.dataset.index],
      searchHistory: [],
      clear: false,
    })

  },

  // 输入搜索内容时触发的事件
  onSearchInput(e) {
    //console.log(e.detail.value)
    // 更新输入的搜索内容
    this.setData({
      searchInput: e.detail.value,
      // 根据输入内容过滤搜索历史显示，使用indexOf代替includes
      searchHistory: this.data.searchHistorytemp.filter(item => item.indexOf(e.detail.value) > -1),
      //clear: true,
    });
    if (this.data.searchHistory.length === 0) {
      this.setData({
        clear: false,
      })
    } else {
      this.setData({
        clear: true,
      })
    }

    //console.log(this.data.searchInput)
  }, zhuanjia() {
    console.log(1)
    this.setData({
      index: 1,
      clear: false,
      showTap: false,
      placeholder: "请输入专家关键词/擅长"
    })

  },
  wenzhang() {
    this.setData({
      index: 2,
      clear: false,
      showTap: false,
      placeholder: "请输入文章关键词"
    })
  },
  tiwen() {
    this.setData({
      index: 3,
      clear: false,
      showTap: false,
      placeholder: "请输入提问关键词"
    })
  },
  // 点击搜索按钮时触发的事件
  onCancel() {
    console.log("搜索")
    if (this.data.searchInput != "") {
      this.data.searchHistorytemp.push(this.data.searchInput)
      this.data.searchHistory.push(this.data.searchInput)
    }
    // 去重
    var temp = this.data.searchHistorytemp
    var temp1 = [...new Set(temp)]
    wx.setStorageSync('searchHistory', temp1)
    //console.log(temp1)
    if (this.data.index === 0) {
      wx.request({
        url: getApp().globalData.baseUrl + '/search/all?keyword=' + this.data.searchInput,
        success: (res) => {
          console.log(res.data.data)
          this.setData({
            result: res.data.data,
            showTap: false,
            showresult: [false, false, false],
          })
          if (this.data.result.consultants.length !== 0) {
            var temp = this.data.showresult
            temp[0] = true
            this.setData({
              showresult: temp
            });
          }
          if (this.data.result.selectedArticles.length !== 0) {
            var temp = this.data.showresult
            temp[1] = true
            this.setData({
              showresult: temp
            });
          }
          if (this.data.result.qaReplies.length !== 0) {
            var temp = this.data.showresult
            temp[2] = true
            this.setData({
              showresult: temp
            });
          }
        }
      })
    }
    if (this.data.index === 1) {
      wx.request({
        url: getApp().globalData.baseUrl + '/search/all?keyword=' + this.data.searchInput,
        success: (res) => {
          this.setData({
            result: res.data.data,
            showTap: false,
            showresult: [false, false, false],
          })
          if (this.data.result.consultants.length !== 0) {
            var temp = this.data.showresult
            temp[0] = true
            this.setData({
              showresult: temp
            });
          }
        }
      })
    }
    if (this.data.index === 2) {
      wx.request({
        url: getApp().globalData.baseUrl + '/search/all?keyword=' + this.data.searchInput,
        success: (res) => {
          this.setData({
            result: res.data.data,
            showTap: false,
            showresult: [false, false, false],
          })
          if (this.data.result.selectedArticles.length !== 0) {
            var temp = this.data.showresult
            temp[1] = true
            this.setData({
              showresult: temp
            });
          }

        }
      })
    }
    if (this.data.index === 3) {
      wx.request({
        url: getApp().globalData.baseUrl + '/search/all?keyword=' + this.data.searchInput,
        success: (res) => {
          this.setData({
            result: res.data.data,
            showTap: false,
            showresult: [false, false, false],
          })
          if (this.data.result.qaReplies.length !== 0) {
            var temp = this.data.showresult
            temp[2] = true
            this.setData({
              showresult: temp
            });
          }
        }
      })
    }

  },

  // 清空搜索历史记录时触发的事件
  onClearHistory() {
    wx.removeStorageSync('searchHistory');
    // 清空搜索历史记录
    this.setData({
      searchHistorytemp: [],
      searchHistory: [],
      clear: false
    });
    // 这里可以添加清除本地存储搜索历史的代码
  },

  // 点击搜索分类时触发的事件
  onSearchCategory(e) {
    // 获取点击的分类
    const category = e.currentTarget.dataset.category;
    // 获取输入的搜索内容，并去除前后空格
    const searchValue = this.data.searchInput.trim();
    // 如果搜索内容不为空，则执行搜索逻辑
    if (searchValue) {
      // 这里可以添加搜索逻辑，根据分类和搜索内容进行搜索
      console.log(`搜索${category}：${searchValue}`);
    }
  },
  onLoad() {
    if (this.data.searchHistory.length === 0) {
      this.setData({
        clear: false
      })
    }
    if (wx.getStorageSync('searchHistory') !== '') {
      var temp = this.data.searchHistory
      temp = wx.getStorageSync('searchHistory')
      this.setData({
        clear: true,
        searchHistory: temp,
        searchHistorytemp: temp
      });
    }
    console.log(this.data.searchHistory)
  },
  onHide() {
    var temp = this.data.searchHistorytemp
    var temp1 = [...new Set(temp)]
    wx.setStorageSync('searchHistory', temp1)
    console.log(temp1)
  }
});